package top.oneit.jdownload.component;

import cn.hutool.core.util.StrUtil;
import javafx.animation.FadeTransition;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
import javafx.stage.Popup;
import javafx.stage.Stage;
import javafx.util.Duration;

import java.nio.charset.StandardCharsets;
import java.util.Timer;
import java.util.TimerTask;


/**
 * @author lingkang
 * @date 2021/11/10
 */
public class JToast {
    private int time = 2;
    private Popup popup = new Popup();
    private Label label = new Label();
    private byte[] msgLen;
    private String msg;

    // 从下面语句创建一个淡入淡出效果对象并设置持续事件为2S
    FadeTransition fadeTransition = new FadeTransition(Duration.millis(800));

    public JToast() {
        init(null);
    }

    private void init(String msg) {
        this.msg = msg;
        popup.setAutoFix(true);
        popup.setAutoHide(false);

        label.setText(msg);
        label.setTextFill(Color.WHITE);//消息字体颜色
        label.setAlignment(Pos.CENTER);//居中
        label.setPadding(new Insets(5, 15, 5, 15));
        label.setStyle("-fx-font-size: 14px;-fx-fill:white;-fx-background-color: #303030;-fx-border-radius: 15px;-fx-background-radius: 15px;");//label透明,圆角
        popup.getContent().add(label);
    }

    public void show(Node node) {
        show(msg, (Stage) node.getScene().getWindow());
    }

    public void show(String msg, Node node) {
        show(msg, (Stage) node.getScene().getWindow());
    }

    public void show(Stage stage) {
        show(msg, stage);
    }

    public void show(String msg, Stage stage) {
        if (StrUtil.isEmpty(msg)){
            msg=" ";
        }
        label.setText(msg);
        msgLen = msg.getBytes(StandardCharsets.UTF_8);
//        popup.setX(stage.getX() + stage.getWidth() / 2 - popup.getWidth() / 2 - msgLen.length * 3.6f);
//        popup.setY(stage.getY() + stage.getHeight() / 2 - popup.getHeight() / 2);

        fadeTransition.setFromValue(0);   // 设置起始透明度为1.0，表示不透明
        fadeTransition.setToValue(1);     // 设置结束透明度为0.0，表示透明
        fadeTransition.setCycleCount(1);     // 设置循环周期为无限
        fadeTransition.setAutoReverse(true);    // 设置自动反转
        fadeTransition.setNode(label);         // 设置动画应用的节点
        fadeTransition.play();                  // 播放动画

        popup.show(stage);

        Timer timer = new Timer();
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                Platform.runLater(() -> {
                    if (popup.isShowing()) {
                        fadeTransition.setFromValue(1);   // 设置起始透明度为1.0，表示不透明
                        fadeTransition.setToValue(0);     // 设置结束透明度为0.0，表示透明
                        fadeTransition.play();                  // 播放动画
                        fadeTransition.setOnFinished(new EventHandler<ActionEvent>() {
                            @Override
                            public void handle(ActionEvent event) {
                                popup.hide();
                            }
                        });
                    }
                });
                timer.cancel();
            }
        };
        timer.schedule(task, time * 1000);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }
}
